//
//  endian.c
//  CppDemo
//
//  Created by 蒋益杰 on 16/10/8.
//  Copyright © 2016年 com.ed. All rights reserved.
//

#include <stdio.h>
#include <stdbool.h>

typedef unsigned char BYTE;

extern _Bool is_big_endian() {
    unsigned int num, *p;
    p = &num;
    num = 0;
    *((BYTE *)p) = 0xff;
    if (num == 0xff) {
        return false;
    } else {
        return true;
    }
}

extern _Bool is_big_endian1() {
    union w {
        unsigned int a;
        unsigned char b;
    } c;
    c.a = 1;
    return (c.b != 1);
}

static union { char c[4]; unsigned long mylong; } endian_test = {{ 'l', '?', '?', 'b' } };
#define MY_ENDIANNESS ((char)endian_test.mylong)

#define uint32 unsigned int
#define uint16 unsigned short int
#define BigToLittleEndian16(x) ((((uint16) (x) & 0xff00) >> 8 ) | \
                                (((uint16) (x) & 0x00ff) << 8 ))

#define BigToLittleEndian32(x) ((((uint32) (x) & 0xff000000) >> 24 ) | \
                                (((uint32) (x) & 0x00ff0000) >> 8 ) | \
                                (((uint32) (x) & 0x0000ff00) << 8 ) | \
                                (((uint32) (x) & 0x000000ff) << 24 ))

